package com.codeoss.framework.codegenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.util.IOUtil;
import com.codeoss.framework.webide.request.ModelFormPageRequest;


public class CodeGeneratorUtil {
	public static String getClassName(Table table) {
		String tableName=table.getTableName();
		String className="";
		String tableNameArray[]=tableName.split("_");
		for(int i=0;i<tableNameArray.length;i++) {
			className+=tableNameArray[i].substring(0,1).toUpperCase()+tableNameArray[i].substring(1);
		}
		return className;
	}
	
	public static String generateAllModel(List<Table> tableList,String basePath) {
		String folderName=UUID.randomUUID().toString();
		for(int i=0;i<tableList.size();i++) {
			Table table=tableList.get(i);
			generate(folderName,basePath,table,true,false,false);
		}
		return folderName;
	}
	
	public static String generate(String basePath,Table table,boolean generateModel,boolean generateDAO,boolean generateService) {
		String folderName=UUID.randomUUID().toString();
		generate(folderName,basePath,table,generateModel,generateDAO,generateService);
		return folderName;
	}
	
	public static String generate(String folderName,String basePath,Table table,boolean generateModel,boolean generateDAO,boolean generateService) {
		String packageName=table.getPackageName();
		String packageFolder=packageName.replace(".", File.separator);
		File folder=new File(basePath+File.separator+"code"+File.separator+folderName+File.separator+packageFolder);
		folder.mkdirs();
		
		String className=getClassName(table);
		
		if(generateModel) {
			ModelGenerator modelGenerate=new ModelGenerator();
			String modelContent=modelGenerate.generate(table);
			File modelFolder=new File(folder,"model");
			modelFolder.mkdir();
			
			File modelFile=new File(modelFolder,className+".java");
			FileOutputStream modelFos=null;
			try {
				modelFos = new FileOutputStream(modelFile);
				modelFos.write(modelContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(modelFos);
			}
		}
		
		if(generateDAO) {
			DaoGenerator daoGenerate=new DaoGenerator();
			String daoContent=daoGenerate.generate(table);
			File daoFolder=new File(folder,"dao");
			daoFolder.mkdir();
			
			File daoFile=new File(daoFolder,className+"Mapper.java");
			FileOutputStream daoFos=null;
			try {
				daoFos = new FileOutputStream(daoFile);
				daoFos.write(daoContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(daoFos);
			}
		}
		
		
		if(generateService) {
			ServiceInterfaceGenerator serviceInterfaceGenerate=new ServiceInterfaceGenerator();
			String serviceInterfaceContent=serviceInterfaceGenerate.generate(table);
			File serviceInterfaceFolder=new File(folder,"service");
			serviceInterfaceFolder.mkdir();
			
			File serviceInterfaceFile=new File(serviceInterfaceFolder,"I"+className+"Service.java");
			FileOutputStream serviceInterfaceFos=null;
			try {
				serviceInterfaceFos = new FileOutputStream(serviceInterfaceFile);
				serviceInterfaceFos.write(serviceInterfaceContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(serviceInterfaceFos);
			}
			
			ServiceImplGenerator serviceImplGenerate=new ServiceImplGenerator();
			String serviceImplContent=serviceImplGenerate.generate(table);
			File serviceImplFolder=new File(serviceInterfaceFolder,"impl");
			serviceImplFolder.mkdir();
			
			File serviceImplFile=new File(serviceImplFolder,className+"Service.java");
			FileOutputStream serviceImplFos=null;
			try {
				serviceImplFos = new FileOutputStream(serviceImplFile);
				serviceImplFos.write(serviceImplContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(serviceImplFos);
			}
		}
		return folderName;
	}
	
	public static String generate(String basePath,Table table,ModelFormPageRequest request) {
		String folderName=UUID.randomUUID().toString();
		String packageName=table.getPackageName();
		String packageFolder=packageName.replace(".", File.separator);
		File folder=new File(basePath+File.separator+"code"+File.separator+folderName+File.separator+packageFolder);
		folder.mkdirs();
		
		String className=getClassName(table);
		
		if(request.isGenerateModel()) {
			ModelGenerator modelGenerate=new ModelGenerator();
			String modelContent=modelGenerate.generate(table);
			File modelFolder=new File(folder,"model");
			modelFolder.mkdir();
			
			File modelFile=new File(modelFolder,className+".java");
			FileOutputStream modelFos=null;
			try {
				modelFos = new FileOutputStream(modelFile);
				modelFos.write(modelContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(modelFos);
			}
		}
		
		if(request.isGenerateDAO()) {
			DaoGenerator daoGenerate=new DaoGenerator();
			String daoContent=daoGenerate.generate(table);
			File daoFolder=new File(folder,"dao");
			daoFolder.mkdir();
			
			File daoFile=new File(daoFolder,className+"Mapper.java");
			FileOutputStream daoFos=null;
			try {
				daoFos = new FileOutputStream(daoFile);
				daoFos.write(daoContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(daoFos);
			}
		}
		
		
		if(request.isGenerateService()) {
			ServiceInterfaceGenerator serviceInterfaceGenerate=new ServiceInterfaceGenerator();
			String serviceInterfaceContent=serviceInterfaceGenerate.generate(table);
			File serviceInterfaceFolder=new File(folder,"service");
			serviceInterfaceFolder.mkdir();
			
			File serviceInterfaceFile=new File(serviceInterfaceFolder,"I"+className+"Service.java");
			FileOutputStream serviceInterfaceFos=null;
			try {
				serviceInterfaceFos = new FileOutputStream(serviceInterfaceFile);
				serviceInterfaceFos.write(serviceInterfaceContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(serviceInterfaceFos);
			}
			
			ServiceImplGenerator serviceImplGenerate=new ServiceImplGenerator();
			String serviceImplContent=serviceImplGenerate.generate(table);
			File serviceImplFolder=new File(serviceInterfaceFolder,"impl");
			serviceImplFolder.mkdir();
			
			File serviceImplFile=new File(serviceImplFolder,className+"Service.java");
			FileOutputStream serviceImplFos=null;
			try {
				serviceImplFos = new FileOutputStream(serviceImplFile);
				serviceImplFos.write(serviceImplContent.getBytes());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeOutputStream(serviceImplFos);
			}
		}
		return folderName;
	}
}
